/*
*
==============================================================================
*  Name        : 
*  Part of     : Omen Game Engine
*  Description : 
*  Version     : 1.0
*
*  Copyright (c) 2013 Lauri Kortevaara.
* 
==============================================================================
*/
#define OMEN_SOURCE 1

/// This source file is only compilable on windows platform
#ifdef _WIN32

/// Omen Headers
#include "OmenFileUtils.h"

using namespace Omen;

// STL Headers
#include <iostream>

// Windows headers
#include <Windows.h>

FileUtils::FileUtils()
{
}

FileUtils::~FileUtils()
{
}

bool FileUtils::findFiles( const std::wstring& searchPath, std::wstring& searchFileName, bool bRecursive )
{
    static int recursionLevel = -1;
    static bool bFound = false;
    recursionLevel++;

    // If were starting a new search, just erase the list of found files.
    if( recursionLevel == 0 )
    {
        bFound = false;
        m_listFoundFiles.clear();
    }

    
    m_bRecursive = bRecursive;

    std::wstring filterPath = searchPath;
    std::wstring filterFileName;
    HANDLE hFind = NULL;

    // Make sure that path is ending with "\\"
    if( filterPath.back() != L'/' && filterPath.back() != L'\\' )
        filterPath.append( L"\\" );
    
    filterFileName = searchFileName;

    // If the filename is only partial, add a * filter
    if( filterFileName.find( L"." ) == std::wstring::npos )
        filterFileName.append( L"*" );

    std::wstring filter = filterPath + filterFileName;
    hFind = FindFirstFile( filter.c_str(), &m_findFileData );
    if( hFind == INVALID_HANDLE_VALUE )
    {
        filter = filterPath;
        filter += L"*.*";
        hFind = FindFirstFile( filter.c_str(), &m_findFileData );
    }
    
    while( hFind != INVALID_HANDLE_VALUE )
    {
        std::wstring fileName = m_findFileData.cFileName;
        std::wstring extMatch = searchFileName;
        if( extMatch.find( L"*." ) != std::wstring::npos )
            extMatch = extMatch.substr( extMatch.find_last_of( L"." ) );
        if( fileName != L"." && fileName != L".." )
        if( fileName == searchFileName || fileName.find(extMatch.c_str(),0,4) != std::wstring::npos )
        {
            m_listFoundFiles.push_back(filterPath+fileName);
            bFound = true;
        }
        if( bRecursive && fileName != L"." && fileName != L".." && (m_findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
            bFound = findFiles( filterPath+fileName, filterFileName );

        if( !FindNextFile( hFind, &m_findFileData ) )
          break;
    }

    if( bFound )
    {
        m_filterPath = filterPath;
        m_filterFileName = filterFileName;
        m_hFind = hFind;
    }
    recursionLevel--;
    return bFound;
}

void FileUtils::getFoundFiles( std::wstring listfiles[], unsigned long maxSize )
{
    maxSize = min(maxSize, m_listFoundFiles.size() );
    for( unsigned long i = 0; i < maxSize; ++i )
        listfiles[i] = m_listFoundFiles.at(i);
}

unsigned long FileUtils::filesFoundCount()
{
    return m_listFoundFiles.size();
}

LONGLONG FileUtils::getFileSize( const std::wstring& filePath )
{
    LONGLONG fileSize = -1;
    return fileSize;
}

bool FileUtils::fileExists( const std::wstring& filePath )
{
    bool bExists = false;
    FILE* f = NULL;
    errno_t error = _wfopen_s( &f, filePath.c_str(), L"r" );
    if( !error )
    {
        bExists = true;
        fclose(f);
    }
    return bExists;
}

#endif // _WIN32

/// EOF